package com.vcg.community.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GenericCode {

	public static void main(String[] args) throws Exception {
		// generator("commerce", "Commerce",
		// "com.vcg.usercenter.user",JDBCUtils.getLocalGicCrmConnection());

		Connection conn = JDBCUtils.getUserCenterConnection();
		List<String> tbls = TblUtil.getTbls(conn);
		for (String table : tbls) {
			String[] split = table.split("_");
			String modelName = "";
			for (String string : split) {
				modelName = modelName + firstUp(string);
			}
			generator(table, modelName, "com.vcg.usercenter.users.model", conn);
		}

	}

	/**
	 * 
	 * @param tableName
	 *            表明
	 * @param modelName
	 *            实体类名,请采用驼峰模式,例如crm_attr_edit : CrmAttrEdit
	 * @param target
	 *            包路径
	 * @throws Exception
	 * 
	 */

	public static void generator(String tableName, String modelName,
			String target, Connection conn) throws Exception {

		String driverName = conn.getMetaData().getDriverName().toLowerCase();

		String sql = "";
		if (driverName.contains("mysql")) {
			sql = "select * from " + tableName + " limit 0,1";
		} else {
			sql = "select top 1 * from " + tableName;
		}

		PreparedStatement statement = conn.prepareStatement(sql);
		ResultSet result = statement.executeQuery();
		ResultSetMetaData metaData = result.getMetaData();

		String path = "src" + "/main/java/" + target.replace(".", "/") + "";
		File dir = new File(path);

		if (dir.exists()) {
			dir.mkdirs();
		}
		// 生成实体类
		getModel(metaData, modelName, target, path);
		getConfig(metaData, modelName, target, path, tableName);
		getDao(metaData, modelName, target, path);
		getService(metaData, modelName, target, path, tableName);
		getServiceImpl(metaData, modelName, target, path, tableName);

	}

	public static void getDao(ResultSetMetaData metaData, String modelName,
			String target, String path) throws IOException, SQLException {
		File file = new File(path.replace("model", "dao"));
		if (!file.exists()) {
			file.mkdirs();
		}

		BufferedWriter bw = new BufferedWriter(new FileWriter(path.replace(
				"model", "dao") + "/" + modelName + "Dao.java"));

		bw.write("package " + target.replace("model", "dao") + ";");
		bw.newLine();
		bw.newLine();

		bw.write("import com.vcg.usercenter.common.base.BaseDao;");
		bw.newLine();

		bw.write("import " + target + "." + modelName + ";");
		bw.newLine();
		bw.newLine();

		bw.write("public interface " + modelName + "Dao extends BaseDao<"
				+ modelName + ">{");
		bw.newLine();
		bw.newLine();

		bw.write("}");
		bw.close();

	}

	public static void getModel(ResultSetMetaData metaData, String modelName,
			String target, String path) throws IOException, SQLException {

		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}

		int columnCount = metaData.getColumnCount();
		Boolean bigdecimal = false;
		Boolean date = false;
		List<String> colunms = new ArrayList<String>();
		for (int i = 1; i <= columnCount; i++) {
			String columnTypeName = metaData.getColumnTypeName(i);
			String columnName = metaData.getColumnName(i);
			String columnClassName = metaData.getColumnClassName(i);
			if (bigdecimal == false) {
				bigdecimal = columnClassName.equals("java.math.BigDecimal");
			}
			if (date == false) {
				date = columnClassName.equals("java.util.Date");
				date = columnClassName.equals("java.sql.Timestamp");
			}

			System.out.println(columnName + "\t" + columnTypeName + "\t"
					+ columnClassName);
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(path + "/"
				+ modelName + ".java"));

		bw.write("package " + target + ";");
		bw.newLine();
		bw.newLine();

		if (bigdecimal) {
			bw.write("import java.math.BigDecimal;");
			bw.newLine();
		}

		if (date) {
			bw.write("import java.util.Date;");
			bw.newLine();
		}

		bw.write("import com.vcg.usercenter.common.base.BaseModel;");
		bw.newLine();
		bw.newLine();

		bw.write("public class " + modelName + " extends BaseModel{");
		bw.newLine();
		bw.newLine();

		bw.write("\tprivate static final long serialVersionUID = 1L;;");
		bw.newLine();
		bw.newLine();
		int columnNameSize = metaData.getColumnCount();

		String typeName = null;
		for (int i = 1; i <= columnNameSize; i++) {
			String columnName = colunmNameToFieldName(metaData.getColumnName(i)
					.toLowerCase());
			colunms.add(columnName);
			String columnClassName = metaData.getColumnClassName(i);
			if (columnClassName.contains("java.lang.Integer")) {
				typeName = "Integer";
			}
			if (columnClassName.contains("java.lang.String")) {
				typeName = "String";
			}
			if (columnClassName.contains("java.sql.Timestamp")) {
				typeName = "Date";
			}
			if (columnClassName.contains("java.math.BigDecimal")) {
				typeName = "BigDecimal";
			}
			if (columnClassName.contains("java.lang.Long")) {
				typeName = "Integer";
			}
			if (columnClassName.contains("java.lang.Short")) {
				typeName = "Short";
			}
			if (columnClassName.contains("java.lang.Float")) {
				typeName = "Float";
			}

			bw.write("	private " + typeName + " " + columnName + ";");
			bw.newLine();
			bw.newLine();

		}

		for (int i = 1; i <= columnNameSize; i++) {

			String columnName = colunmNameToFieldName(metaData.getColumnName(i)
					.toLowerCase());

			String columnClassName = metaData.getColumnClassName(i);

			if (columnClassName.contains("java.lang.Integer")) {
				typeName = "Integer";
			}
			if (columnClassName.contains("java.lang.String")) {
				typeName = "String";
			}
			if (columnClassName.contains("java.sql.Timestamp")) {
				typeName = "Date";
			}
			if (columnClassName.contains("java.math.BigDecimal")) {
				typeName = "BigDecimal";
			}
			if (columnClassName.contains("java.lang.Long")) {
				typeName = "Integer";
			}
			if (columnClassName.contains("java.lang.Short")) {
				typeName = "Short";
			}
			if (columnClassName.contains("java.lang.Float")) {
				typeName = "Float";
			}

			bw.write("	public " + typeName + " "
					+ colunmNameToFieldName("get_" + columnName) + "() {");
			bw.newLine();

			bw.write("		return " + columnName + ";");
			bw.newLine();

			bw.write("	}");
			bw.newLine();
			bw.newLine();

			bw.write("	public void "
					+ colunmNameToFieldName("set_" + columnName) + "("
					+ typeName + " " + columnName + ") {");
			bw.newLine();

			bw.write("		this." + columnName + " = " + columnName + ";");
			bw.newLine();

			bw.write("	}");
			bw.newLine();
			bw.newLine();

		}

		// bw.write("	public String toString() {");
		// bw.newLine();

		// String toString="";
		// for (String c : colunms) {
		// toString=toString+"\\"+"\""+c+":+"+"\\\""+"\"+"+c+"+\"";
		// }
		// bw.write("		return \"{"+toString+"}\"");
		// bw.newLine();

		// bw.write("	}");

		bw.write("}");
		bw.newLine();

		bw.close();
	}
	
	
	public static void getService(ResultSetMetaData metaData, String modelName,
			String target, String path, String tableName) throws IOException{
		
		
		File file = new File(path.replace("model", "service"));
		if (!file.exists()) {
			file.mkdirs();
		}

		BufferedWriter bw = new BufferedWriter(new FileWriter(path.replace(
				"model", "service") + "/" + modelName + "Service.java"));

		bw.write("package " + target.replace("model", "service") + ";");
		bw.newLine();
		bw.newLine();

		bw.write("import com.vcg.usercenter.common.base.BaseService;");
		bw.newLine();

		bw.write("import " + target + "." + modelName + ";");
		bw.newLine();
		bw.newLine();

		bw.write("public interface " + modelName + "Service extends BaseService<"+ modelName + ">{");
		bw.newLine();
		bw.newLine();
		
		

		bw.write("}");
		bw.close();

		
		
	}
	
	
	public static void getServiceImpl(ResultSetMetaData metaData, String modelName,
			String target, String path, String tableName) throws IOException{
		
		
		File file = new File(path.replace("model", "service/impl"));
		if (!file.exists()) {
			file.mkdirs();
		}

		BufferedWriter bw = new BufferedWriter(new FileWriter(path.replace(
				"model", "service") + "/impl/" + modelName + "ServiceImpl.java"));

		bw.write("package " + target.replace("model", "service") + ".impl;");
		bw.newLine();
		bw.newLine();
		
		bw.write("import org.springframework.beans.factory.annotation.Autowired;");
		bw.newLine();
		bw.write("import org.springframework.stereotype.Service;");
		bw.newLine();
		bw.newLine();

		bw.write("import com.vcg.usercenter.common.base.BaseServiceImpl;");
		bw.newLine();

		bw.write("import " + target + "." + modelName + ";");
		bw.newLine();
		
		bw.write("import " + target.replace("model", "dao") + "." + modelName + "Dao;");
		bw.newLine();
		
		bw.write("import " + target.replace("model", "service") + "." + modelName + "Service;");
		bw.newLine();
		
		bw.newLine();

		bw.write("@Service(\""+firstLow(modelName)+"Service\")");
		bw.newLine();
		
		bw.write("public class " + modelName + "ServiceImpl extends BaseServiceImpl<"+ modelName + "> implements "+modelName+"Service {");
		bw.newLine();
		bw.newLine();
		
		bw.write("\t@Autowired");
		bw.newLine();
		
		bw.write("\tprivate "+modelName+"Dao "+firstLow(modelName)+"Dao;" );
		bw.newLine();
		bw.newLine();
		

		bw.write("\t@Override");
		bw.newLine();

		
		bw.write("\tpublic void setDao() {");
		bw.newLine();
		
		bw.write("		this.baseDao = "+firstLow(modelName)+"Dao;");
		bw.newLine();
		
		bw.write("\t}");
		bw.newLine();
		bw.newLine();
		

		bw.write("}");
		bw.close();

		
		
	}

	public static void getConfig(ResultSetMetaData metaData, String modelName,
			String target, String path, String tableName) throws IOException,
			SQLException {
		String configName = modelName.replace(".model", ".dao") + "Dao";
		String[] split = tableName.toLowerCase().split("_");
		if (split.length >= 1) {
			configName = split[0] + "_";
		}
		for (int i = 1; i < split.length; i++) {
			configName = configName + split[i];
		}
		File file = new File(path.replace("/model", "/dao").replace("java",
				"resources"));
		if (!file.exists()) {
			file.mkdirs();
		}
		System.out.println(file.getAbsolutePath());
		BufferedWriter bw = new BufferedWriter(new FileWriter(
				file.getAbsolutePath() + "/" + modelName + "Dao.xml"));

		int columnCount = metaData.getColumnCount();
		// String className=target+"."+modelName;
		bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		bw.newLine();

		bw.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">");
		bw.newLine();
		bw.newLine();

		bw.write("<mapper namespace=\"" + target.replace("model", "dao") + "."
				+ modelName + "Dao" + "\" >");
		bw.newLine();
		bw.newLine();
		
		String resultMap=firstLow(modelName)+"ResultMap";

		bw.write("\t<resultMap id=\"" + resultMap + "\" type=\""+modelName+"\" >");
		bw.newLine();

		String clounmNamesStr = "\t\t";
		String clounmNamesStr2 = "\t\t";

		List<String> colunmList = new ArrayList<String>();
		List<String> colunmsList2=new ArrayList<String>();
		for (int i = 1; i <= columnCount; i++) {

			String columnTypeName = metaData.getColumnTypeName(i);
			String columnName = metaData.getColumnName(i);
			colunmsList2.add(columnName);
			String propertyName = colunmNameToFieldName(metaData.getColumnName(
					i).toLowerCase());
			colunmList.add(colunmNameToFieldName(columnName));
			if (columnTypeName.equals("INTEGER UNSIGNED")) {
				columnTypeName = "INTEGER";
			}

			clounmNamesStr = clounmNamesStr + columnName + ","+"\t\t";
			clounmNamesStr2 = "#{" + clounmNamesStr2 + columnName + "},";
			if (i % 2 == 0) {
				clounmNamesStr = clounmNamesStr + "\r\n" + "\t\t";
				clounmNamesStr2 = clounmNamesStr + "\r\n" + "\t\t";
			}
			bw.write("		<result property=\"" + propertyName + "\" column=\""
					+ columnName + "\" />");
			bw.newLine();
		}
		bw.write("\t</resultMap>");
		bw.newLine();
		bw.newLine();
		
		bw.write("\t<!-- 根据id查询对象 -->");
		bw.newLine();
		
		bw.write("\t<select id=\"getById\" parameterType=\"integer\" resultMap=\""+resultMap+"\">");
		bw.newLine();
		
		
		bw.write("\t\t\t<include refid=\"selector\" />");
		bw.newLine();
		
		bw.write("\t\twhere");
		bw.newLine();
		
		bw.write("\t\t\t"+colunmsList2.get(0)+"=#{"+colunmList.get(0)+"}");
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();

		
		bw.write("\t<!-- 查询所有 -->");
		bw.newLine();
		
		bw.write("\t<select id=\"getAll\" resultMap=\""+resultMap+"\">");
		bw.newLine();
		
		bw.write("\t\t<include refid=\"selector\" />");
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();
		
		
		bw.write("\t<!-- 根据条件查询对象 -->");
		bw.newLine();
		
		bw.write("\t<select id=\"getByMapCondition\" parameterType=\"hashMap\" resultMap=\""+resultMap+"\">");
		bw.newLine();
		
		bw.write("\t\t<include refid=\"selector\" />");
		bw.newLine();
		
		bw.write("\t\t<include refid=\"wherePropertyNotNull\" />");
		bw.newLine();
		
		bw.write("\t\t<include refid=\"orderBy\" />");
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();
		
		bw.write("\t<!-- 分页 -->");
		bw.newLine();
		
		
		bw.write("\t<select id=\"getResultWithPage\" parameterType=\"hashMap\" resultMap=\""+resultMap+"\">");
		bw.newLine();
		
		bw.write("\t\t<include refid=\"selector\" />");
		bw.newLine();
		
		bw.write("\t\t<include refid=\"wherePropertyNotNull\" />");
		bw.newLine();

		
		bw.write("\t\t<include refid=\"orderBy\" />");
		bw.newLine();
		
		bw.write("\t\tlimit #{startNum},#{endNum}");
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();
		
		
		bw.write("\t<!-- 插入 -->");
		bw.newLine();
		
		bw.write("\t<insert id=\"insert\" parameterType=\""+modelName+"\" useGeneratedKeys=\"true\" keyProperty=\"pk\">");
		bw.newLine();
		
		bw.write("\t\tinsert into");
		bw.newLine();
		
		bw.write("\t\t\t"+tableName+"(");
		bw.write("<include refid=\"colunms\" />)");
		bw.newLine();
		
		bw.write("\t\tvalues");
		bw.newLine();
		
		bw.write("\t\t\t(<include refid=\"propertys\" />)");
		bw.newLine();
		
		bw.write("\t</insert>");
		bw.newLine();
		bw.newLine();
		
		
		bw.write("\t<!-- 根据主键更新 -->");
		bw.newLine();
		
		bw.write("\t<update id=\"update\" parameterType=\""+modelName+"\">");
		bw.newLine();
		
		bw.write("\t\tupdate");
		bw.newLine();
		
		bw.write("\t\t\t"+tableName+"");
		bw.newLine();
		
		bw.write("\t\tset");
		bw.newLine();
		
		bw.write("\t\t\t<include refid=\"setPropertys" +"\" />");
		bw.newLine();
		
		
		bw.write("\t\twhere");
		bw.newLine();
		
		bw.write("\t\t\t"+colunmsList2.get(0)+"=#{"+colunmList.get(0)+"}");
		bw.newLine();
		
		
		
		bw.write("\t</update>");
		bw.newLine();
		bw.newLine();
		
		
		
//		bw.write("\t<!-- 根据条件更新 -->");
//		bw.newLine();
//		
//		bw.write("\t<update id=\"updateByMapCondition\" parameterType=\"hashMap\">");
//		bw.newLine();
//		
//		bw.write("\t\tupdate");
//		bw.newLine();
//		
//		bw.write("\t\t\t"+tableName+"");
//		bw.newLine();
//		
//		bw.write("\t\t<set>");
//		bw.newLine();
//		
//		bw.write("\t\t\t<include refid=\"colunmNotNull\" />");
//		bw.newLine();
//		
//		bw.write("\t\t</set>");
//		bw.newLine();
//		
//		bw.write("\t\t\t<include refid=\"wherePropertyNotNull\" />");
//		bw.newLine();
//		
//		
//		
//		bw.write("\t</update>");
//		bw.newLine();
//		bw.newLine();
		
		
			
		bw.write("\t<!-- 统计总条数 -->");
		bw.newLine();
		
		bw.write("\t<select id=\"count\" resultType=\"long\">");
		bw.newLine();
		
		bw.write("\t\tselect count(1) from "+tableName);
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();
		
		
		
		bw.write("	<!-- 根据条件统计总条数 -->");
		bw.newLine();
		
		bw.write("\t<select id=\"countByCondition\" parameterType=\"hashMap\" resultType=\"long\">");
		bw.newLine();
		
		bw.write("\t\tselect count(1) from "+tableName);
		bw.newLine();
		
		bw.write("\t\t<include refid=\"wherePropertyNotNull\" />");
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();
		
		
		
		bw.write("\t<!-- 执行sql -->");
		bw.newLine();
		
		bw.write("\t<select id=\"executeSql\" resultType=\"string\">");
		bw.newLine();
		
		bw.write("\t\t${sql}");
		bw.newLine();
		
		bw.write("\t</select>");
		bw.newLine();
		bw.newLine();
		
		
		
		
		bw.write("	<sql id=\"selector\">");
		bw.newLine();
		
		bw.write("\t\tselect");
		bw.newLine();
		
		
		
		bw.write("\t\t\t\t<include refid=\"colunms\" />");
		bw.newLine();
		
		
		bw.write("\t\tfrom");
		bw.newLine();
		
		bw.write("\t\t\t"+tableName);
		bw.newLine();
		
		bw.write("\t</sql>");
		bw.newLine();
		bw.newLine();
		

		bw.write("	<sql id=\"colunms\">");
		bw.newLine();

		String _clounmNamesStr = clounmNamesStr.trim().substring(0,
				clounmNamesStr.trim().length() - 1);
		bw.write("\t\t" + _clounmNamesStr);
		bw.newLine();

		bw.write("\t</sql>");
		bw.newLine();
		bw.newLine();

		bw.write("\t<sql id=\"propertys\">");
		bw.newLine();

		for (int i = 0; i < colunmList.size(); i++) {
			if (i % 2 == 0 && i != 0) {
				bw.newLine();
			}
			
			if(i==(colunmList.size()-1)){
				bw.write("\t\t#{" + colunmList.get(i) + "}");
				continue;
			}
			
			bw.write("\t\t#{" + colunmList.get(i) + "},");
			

		}
		bw.newLine();
		bw.write("\t</sql>");
		bw.newLine();
		bw.newLine();

		bw.write("	<sql id=\"wherePropertyNotNull\">");
		bw.newLine();

		bw.write("\t\t<where>");
		bw.newLine();

		for (int i = 0; i < colunmList.size(); i++) {
			bw.write("\t\t\t<if test=\"" + colunmList.get(i) + "!=null and "+ colunmList.get(i) +"!='' \">");
			bw.newLine();

			bw.write("\t\t\t\tand " + colunmsList2.get(i) + " = #{"
					+ colunmList.get(i) + "}");
			bw.newLine();

			bw.write("\t\t\t</if>");
			bw.newLine();
		}

		bw.write("\t\t</where>");
		bw.newLine();

		bw.write("\t</sql>");
		bw.newLine();
		bw.newLine();

//		bw.write("\t<sql id=\"setPropertysNotNull\">");
//		bw.newLine();
//
//		for (int i = 0; i < colunmList.size(); i++) {
//			bw.write("\t\t<if test=\"" + colunmList.get(i) + "!=null \">");
//			bw.newLine();
//
//			if(i==(colunmList.size()-1)){
//				bw.write("\t\t\t" + colunmsList2.get(i) + " = #{" + colunmList.get(i)
//						+ "}");
//				bw.newLine();
//
//				bw.write("\t\t</if>");
//				bw.newLine();
//				continue;
//			}
//			bw.write("\t\t\t" + colunmsList2.get(i) + " = #{" + colunmList.get(i)
//					+ "},");
//			bw.newLine();
//
//			bw.write("\t\t</if>");
//			bw.newLine();
//		}
//
//		bw.write("\t</sql>");
//		bw.newLine();
//		bw.newLine();

//		bw.write("	<sql id=\"propertyValuesNotNull\">");
//		bw.newLine();
//
//		for (int i = 0; i < colunmList.size(); i++) {
//			bw.write("\t\t<if test=\"" + colunmList.get(i) + "!=null \">");
//			bw.newLine();
//
//			if((colunmList.size()-1)==i){
//				bw.write("\t\t\t" + "#{" + colunmList.get(i) + "}");
//				bw.newLine();
//				bw.write("\t\t</if>");
//				bw.newLine();
//				continue;
//			}
//			
//			bw.write("\t\t\t" + "#{" + colunmList.get(i) + "},");
//			bw.newLine();
//
//			bw.write("\t\t</if>");
//			bw.newLine();
//		}
//
//		bw.write("\t</sql>");
//		bw.newLine();
//		bw.newLine();
//		
		

		bw.write("	<sql id=\"setPropertys\">");
		bw.newLine();


		bw.write("\t\t<trim suffixOverrides=\",\">");
		bw.newLine();
		
		for (int i = 0; i < colunmList.size(); i++) {
			
			bw.write("\t\t\t<if test=\""+colunmList.get(i)+"!=null and "+colunmList.get(i)+"!=''\">");
			bw.newLine();
			bw.write("\t\t\t\t" + colunmsList2.get(i) + " = #{"
					+ colunmList.get(i) + "},");
			bw.newLine();
			bw.write("\t\t\t</if>");
			bw.newLine();

		}
		bw.write("\t\t</trim>");
		bw.newLine();

		bw.write("\t</sql>");
		bw.newLine();
		bw.newLine();
		
		

//		bw.write("	<sql id=\"colunmNotNull\">");
//		bw.newLine();
//
//		for (int i = 0; i < colunmsList2.size(); i++) {
//			bw.write("\t\t<if test=\"" + colunmList.get(i) + "!=null \">");
//			bw.newLine();
//
//			if((colunmsList2.size()-1)==i){
//				bw.write("\t\t\t" + colunmsList2.get(i));
//				bw.newLine();
//				bw.write("\t\t</if>");
//				bw.newLine();
//				continue;
//			}
//			
//			
//			bw.write("\t\t\t" + colunmsList2.get(i)+",");
//			bw.newLine();
//
//			bw.write("\t\t</if>");
//			bw.newLine();
//		}
//
//		bw.write("\t</sql>");
//		bw.newLine();
//		bw.newLine();
		

		bw.write("\t<sql id=\"orderBy\">");
		bw.newLine();

		bw.write("		<if test=\"orderBy!=null\">");
		bw.newLine();

		bw.write("			order by ${orderBy}");
		bw.newLine();

		bw.write("		</if>");
		bw.newLine();

		bw.write("	</sql>");
		bw.newLine();
		bw.newLine();

		bw.write("</mapper>");

		bw.close();
	}

	public static String colunmNameToFieldName(String tblName) {
		String[] split = tblName.split("_");
		String _str = split[0];

		for (int i = 1; i < split.length; i++) {
			_str += firstUp(split[i]);
		}

		return _str;

	}

	public static String firstUp(String str) {

		if (str == null) {
			return null;
		}
		if (str.equals("")) {
			return "";
		}

		if (str.length() == 1) {
			return str.toUpperCase();
		}

		String first = str.substring(0, 1).toUpperCase();
		String last = str.substring(1);

		return first + last;
	}

	public static String firstLow(String str) {

		if (str == null) {
			return null;
		}
		if (str.equals("")) {
			return "";
		}

		if (str.length() == 1) {
			return str.toUpperCase();
		}

		String first = str.substring(0, 1).toLowerCase();
		String last = str.substring(1);

		return first + last;
	}
}
